package com.atguigu.gmall.order.config;

import com.atguigu.gmall.common.constant.MqConst;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class CancelOrderConfig {

    @Bean  //创建消息队列
    public Queue delayQueue(){
        return new Queue(MqConst.QUEUE_ORDER_CANCEL,true);
    }

    @Bean  //创建交换机
    public CustomExchange delayExchange(){
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type","direct");
        return new CustomExchange(
                MqConst.EXCHANGE_DIRECT_ORDER_CANCEL,
                "x-delayed-message",
                true,
                false,
                args);
    }

    @Bean   //绑定routingKey
    public Binding bindingDelay(@Qualifier(value ="delayQueue") Queue delayQueue,@Qualifier(value = "delayExchange") CustomExchange delayExchange){
        return BindingBuilder
                .bind(delayQueue)
                .to(delayExchange)
                .with(MqConst.ROUTING_ORDER_CANCEL)
                .noargs();
    }

}
